package com.zq.im.fragment;

import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.zq.im.R;
import com.zq.im.activity.SettingActivity;
import com.zq.im.api.UserApi;
import com.zq.im.bean.BaseModel;
import com.zq.im.bean.LoginBean;
import com.zq.im.bean.param.UploadImgParam;
import com.zq.im.retrofit.BaseCallBack;
import com.zq.im.retrofit.RetrofitManager;
import com.zq.im.utils.Base64Util;
import com.zq.im.utils.GlideEngine;
import com.zq.im.utils.SharePreferenceUtils;

import java.io.File;

import androidx.annotation.RequiresApi;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by 张强 869518570@qq.com on 2020/8/25
 */
public class MeFragment extends BaseFragment {

    @BindView(R.id.name)
    TextView name;

    @BindView(R.id.icon)
    ImageView icon;

    @Override
    protected int getLayoutId() {
        return R.layout.me_fragment_layout;
    }

    @Override
    protected void initView() {

    }

    @OnClick(R.id.icon)
    void uploadIcon() {
        PictureSelector.create(mActivity)
                .openGallery(PictureMimeType.ofImage())
                .isCompress(true) //压缩图片
                .compressQuality(50)
                .isEnableCrop(true)
                .isCamera(false)
                .maxSelectNum(1)
                .circleDimmedLayer(true)
                .cropImageWideHigh(200, 200)
                .circleDimmedLayer(true)
                .cutOutQuality(100)
                .loadImageEngine(GlideEngine.createGlideEngine()) // Please refer to the Demo GlideEngine.java
                .forResult(PictureConfig.CHOOSE_REQUEST);

    }


    @Override
    protected void initData() {
        name.setText(SharePreferenceUtils.getString("account"));

    }

    @OnClick(R.id.setting)
    void setting() {
        startActivity(new Intent(mActivity, SettingActivity.class));
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    public void uploadImg(String path) {
        String userId = SharePreferenceUtils.getString("userId");
        UserApi api = RetrofitManager.getRetrofit().create(UserApi.class);
        String faceData = Base64Util.ImageToBase64ByLocal(path);
        UploadImgParam param = new UploadImgParam(userId, faceData);
        Gson gson = new Gson();
        String obj = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        Call<BaseModel<LoginBean>> uploadImg = api.uploadImg(body);
        uploadImg.enqueue(new BaseCallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean bean) {

            }

            @Override
            public void onFailure(Throwable t) {

            }
        });

    }
}
